# -*- coding: utf-8 -*-
# mongo 的一些操作

from pymongo import MongoClient
# import json
from redis_ import RedisArchive

redis_client = RedisArchive()

with open('../config.py', 'r', encoding="utf-8") as f:
    exec(f.read())

mongo_ip = mongoinfo['ip']
mongo_port = mongoinfo['port']


class MongoArchive:
    def __init__(self):
        self.conn_ = MongoClient(mongo_ip, mongo_port)
        # conn_ = MongoClient("192.168.1.43", mongoinfo['port'])
        self.acvs_ = self.conn_.guidang4.acvs
        self.capture_ = self.conn_.guidang4.capture
        self.cardacvs_ = self.conn_.guidang4.cardacvs
        self.cardface_ = self.conn_.guidang4.cardface
        self.cardcapture_ = self.conn_.guidang4.cardcapture
        self.faceacvs_ = self.conn_.guidang4.faceacvs
        self.acvsacvs_ = self.conn_.guidang4.acvsacvs
        self.record_ = self.conn_.guidang4.record
        self.card_ = self.conn_.guidang4.card
        self.acvstree_ = self.conn_.guidang4.acvstree
        self.addacvs_img_ = self.conn_.guidang4.addacvs_img
        self.logbase_ = self.conn_.guidang4.logBase
        self.logcount_ = self.conn_.guidang4.logcount
        self.log_count()

    def log_count(self):
        if self.logcount_.find_one({'_id': 'logCount'}) is None:
            self.logcount_.insert_one({"_id": "logCount", "count": 1})

    # 插入logbase
    def ins_logbase(self, log):
        dic = {
            '_id': self.logcount_.find_one_and_update({'_id': "logCount"}, {'$inc': {'count': 1}}, upsert=True).get(
                'count'),
            'log': log
        }
        self.logbase_.insert(dic)

    # 插入建档的图片id
    def insert_add_img(self, face_img):
        self.addacvs_img_.insert_one(face_img)

    # 关联度 记录档案关系tree 进档和档档   ——放在判断档里没有之后 有则不需要增加数量
    def acvs_tree_count(self, acvs_id):
        img = self.acvstree_.find_one({'_id': acvs_id})
        self.acvstree_.update_one({'_id': acvs_id}, {'$inc': {'count': 1}})
        # 升级后计算count 确保父count大于子count
        if img['p'] is not None:
            img_p = self.acvstree_.find_one({'_id': img['p']})
            if img['count'] + 1 > img_p['count']:
                # 三个设置
                self.acvstree_.update_one({'_id': acvs_id}, {'$set': {'p': None}})
                self.update_acvs_p(acvs_id, None)
                self.acvstree_.update_one({'_id': img['p']}, {'$set': {'p': acvs_id}})
                self.update_acvs_p(img['p'], acvs_id)

                self.acvstree_.update_many({'p': img['p']}, {'$set': {'p': acvs_id}})
                self.update_p_p(img['p'], acvs_id)

    def acvs_tree_build(self, acvs_id, acvs):
        # 放在建档加入冗余
        dic = {
            '_id': acvs_id,
            'p': None,
            'count': 0,
            'correlation': [],
            'face_image_uri': acvs['face_image_uri'],
            'f_id': acvs['f_id']
        }
        self.acvstree_.insert_one(dic)

    #  acvs faceacvs cardacvs 加入新的p
    def update_acvs_p(self, acvs_id, p_id):
        self.acvs_.update_one({'_id': acvs_id}, {'$set': {'p': p_id}})
        self.faceacvs_.update_many({'acvs_id': acvs_id}, {'$set': {'p': p_id}})
        self.cardacvs_.update_many({'acvs_id': acvs_id}, {'$set': {'p': p_id}})

    def update_p_p(self, p_id, new_p_id):
        self.acvs_.update_many({'p': p_id}, {'$set': {'p': new_p_id}})
        self.faceacvs_.update_many({'p': p_id}, {'$set': {'p': new_p_id}})
        self.cardacvs_.update_many({'p': p_id}, {'$set': {'p': new_p_id}})
        self.ins_log(new_p_id)

    def ins_log(self, acvs_p_id):
        all_acvs_img = self.acvs_.find({'p': acvs_p_id})
        acvs_list = [x["_id"] for x in all_acvs_img]
        if acvs_p_id in acvs_list:
            acvs_list.remove(acvs_p_id)
        redis_client.rdb_queue_log_merge_acvs(acvs_p_id, acvs_list)

    def p_max_one(self, imgf_p_img, imgs_p, imgs_p_img, imgf, imgf_p):
        #  imgf_p_img fp的img, imgs_p sp的id, imgs_p_img, imgf f的img, imgf_p fp的id
        # 二者合一 imgs_p_img最大
        self.acvstree_.update_one({'_id': imgf_p}, {'$set': {'p': imgs_p}})
        self.update_acvs_p(imgf_p, imgs_p)
        self.acvstree_.update_many({'p': imgf_p}, {'$set': {'p': imgs_p}})
        self.update_p_p(imgf_p, imgs_p)

    def equal_allhas(self, max_count_sub, imgs, imgf, imgs_p, imgf_p):
        if imgs_p != imgf_p:
            # 都有父 且 两个不相等 获取两个父的信息 两个父 都不在有父
            imgs_p_img = self.acvstree_.find_one({'_id': imgs_p})
            imgf_p_img = self.acvstree_.find_one({'_id': imgf_p})
            # if not imgs_p_img['p'] and not imgf_p_img['p']:  # 可以注掉
            # 先比较两个p count
            max_count_p = max(imgs_p_img['count'], imgf_p_img['count'])

            if imgs_p_img['count'] == max_count_p:
                self.p_max_one(imgf_p_img, imgs_p, imgs_p_img, imgf, imgf_p)

            elif imgf_p_img['count'] == max_count_p:
                self.p_max_one(imgs_p_img, imgf_p, imgf_p_img, imgs, imgs_p)

        else:
            # 都有父 且两个父 相等 不需改变
            pass

    @staticmethod
    def cor_cor(img1, img2):
        cor_imgs = img1['correlation']
        if len(cor_imgs) == 0:
            cor_imgs.append({})
        if img2['_id'] in cor_imgs[0]:
            cor_imgs[0][img2['_id']] += 1
        else:
            cor_imgs[0][img2['_id']] = 1

    def cor_s_f(self, imgs, imgf):
        self.cor_cor(imgs, imgf)
        self.cor_cor(imgf, imgs)

    def one_has(self, imgf, imgs_p):
        # s_p f_none
        imgs_p_img = self.acvstree_.find_one({'_id': imgs_p})
        if imgf['_id'] == imgs_p:
            return
        #
        if imgf['count'] > imgs_p_img['count']:
            self.acvstree_.update_one({'_id': imgs_p}, {'$set': {'p': imgf['_id']}})
            self.update_acvs_p(imgs_p, imgf['_id'])
            self.acvstree_.update_many({'p': imgs_p}, {'$set': {'p': imgf['_id']}})
            self.update_p_p(imgs_p, imgf['_id'])
        else:
            self.acvstree_.update_one({'_id': imgf['_id']}, {'$set': {'p': imgs_p}})
            self.update_acvs_p(imgf['_id'], imgs_p)
            self.update_p_p(imgf['_id'], imgs_p)

    def acvs_tree(self, acvssrc, acvsfllow):
        imgs = self.acvstree_.find_one({'_id': acvssrc})
        imgf = self.acvstree_.find_one({'_id': acvsfllow})
        # 记录新增档档关系
        self.cor_s_f(imgs, imgf)
        self.acvstree_.update_one({'_id': acvssrc}, {'$set': {'correlation': imgs['correlation']}})
        self.acvstree_.update_one({'_id': acvsfllow}, {'$set': {'correlation': imgf['correlation']}})

        # 获取两个档案的p
        imgs_p = imgs['p']
        imgf_p = imgf['p']
        max_count_sub = max(imgs['count'], imgf['count'])
        if imgs_p:
            if imgf_p:
                self.equal_allhas(max_count_sub, imgs, imgf, imgs_p, imgf_p)
            else:
                # f档案没有父 比较s档案父 和 f档案的count
                self.one_has(imgf, imgs_p)

        else:
            if imgf_p:
                self.one_has(imgs, imgf_p)
            else:
                # 两个都没有父
                if max_count_sub == imgs['count']:
                    self.acvstree_.update_one({'_id': imgf['_id']}, {'$set': {'p': imgs['_id']}})
                    self.update_acvs_p(imgf['_id'], imgs['_id'])
                    self.update_p_p(imgf['_id'], imgs['_id'])

                elif max_count_sub == imgf['count']:
                    self.acvstree_.update_one({'_id': imgs['_id']}, {'$set': {'p': imgf['_id']}})
                    self.update_acvs_p(imgs['_id'], imgf['_id'])
                    self.update_p_p(imgs['_id'], imgf['_id'])

    def insert_record_ebn(self, faceid, dic):
        return self.record_.update_one({'_id': faceid}, {'$set': dic}, upsert=True)

    @staticmethod
    def dic_make(name, face_id, acvs_id, hit_faceid, similarity):
        dic = {
            "_id": name,
            "face_ids": face_id,
            "face_idt": hit_faceid,
            "acvs_id": acvs_id,
            "similarity": similarity,
            "score": 1
        }
        return dic

    def up_acvslen1(self, acvssrc, acvsfllow, count):
        num = 1 if count == 0 else -1
        self.acvs_.update_one({'_id': acvssrc}, {"$inc": {'acvslen1': num}}, upsert=True)
        self.acvs_.update_one({'_id': acvsfllow}, {"$inc": {'acvslen1': num}}, upsert=True)

    def up_acvslen2(self, acvssrc, acvsfllow, count):
        num = 1 if count == 0 else -1
        self.acvs_.update_one({'_id': acvssrc}, {"$inc": {'acvslen2': num}}, upsert=True)
        self.acvs_.update_one({'_id': acvsfllow}, {"$inc": {'acvslen2': num}}, upsert=True)

    def up_acvslen3(self, acvssrc, acvsfllow, count):
        num = 1 if count == 0 else -1
        self.acvs_.update_one({'_id': acvssrc}, {"$inc": {'acvslen3': num}}, upsert=True)
        self.acvs_.update_one({'_id': acvsfllow}, {"$inc": {'acvslen3': num}}, upsert=True)

    @staticmethod
    def dic_dic(idname, count, acvssrc, acvsfllow):
        dic = {
            "_id": idname,
            "score": count,
            "acvssrc": acvssrc,
            "acvsfllow": acvsfllow
        }
        return dic

    def acvsacvs(self, acvssrc, acvsfllow, count, level):
        # 双向修改acvs_
        idname1 = '%s_%s' % (acvssrc, acvsfllow)
        idname2 = '%s_%s' % (acvsfllow, acvssrc)  # 双向时使用
        img = self.acvsacvs_.find_one({'_id': idname1})
        # img2 = self.acvsacvs_.find_one({'_id': idname2})
        if img:
            levelsrc = int(str(img['score'])[:-5])
            if level == levelsrc:
                self.acvsacvs_upcount(idname1, count)
                self.acvsacvs_upcount(idname2, count)
            else:
                if level == 1:
                    if levelsrc == 2:
                        self.up_acvslen2(acvssrc, acvsfllow, 1)
                        self.up_acvslen1(acvssrc, acvsfllow, 0)
                        self.acvsacvs_upcount(idname1, count)
                        self.acvsacvs_upcount(idname2, count)
                    if levelsrc > 2:
                        self.up_acvslen3(acvssrc, acvsfllow, 1)
                        self.up_acvslen1(acvssrc, acvsfllow, 0)
                        self.acvsacvs_upcount(idname1, count)
                        self.acvsacvs_upcount(idname2, count)

                if level == 2:
                    if levelsrc > 2:
                        self.up_acvslen3(acvssrc, acvsfllow, 1)
                        self.up_acvslen2(acvssrc, acvsfllow, 0)
                        self.acvsacvs_upcount(idname1, count)
                        self.acvsacvs_upcount(idname2, count)
                    # if levelsrc == level:
                    #     acvsacvs_upcount(idname1, count)
                    #     acvsacvs_upcount(idname2, count)

                else:
                    self.acvsacvs_upcount(idname1, count)
                    self.acvsacvs_upcount(idname2, count)

        if not img:
            dic1 = self.dic_dic(idname1, count, acvssrc, acvsfllow)
            dic2 = self.dic_dic(idname2, count, acvsfllow, acvssrc)

            self.acvsacvs_.insert_one(dic1)
            self.acvsacvs_.insert_one(dic2)

            self.acvs_.find_one_and_update({'_id': acvssrc}, {"$inc": {'lenacvs': 1}}, upsert=True)
            self.acvs_.find_one_and_update({'_id': acvsfllow}, {"$inc": {'lenacvs': 1}}, upsert=True)
            level_num = level == 1 and 1 or level == 2 and 2 or level >= 3 and 3 or None
            if level_num:
                up_func = self.__getattribute__("up_acvslen%s" % level_num)
                up_func(acvssrc, acvsfllow, 0)

    def acvsacvs_upcount(self, idname, count):
        self.acvsacvs_.update_one({'_id': idname}, {'$set': {'score': count}}, upsert=True)

    def faceacvs_upcount(self, idname, count):
        self.faceacvs_.update_one({'_id': idname}, {'$set': {'score': count}}, upsert=True)

    def acvs_uplen1(self, acvs_id, count):
        num = 1 if count == 0 else -1
        img = self.acvs_.find_one_and_update({'_id': acvs_id}, {"$inc": {'len1': num}}, upsert=True)
        img['len1'] = img['len1'] + num
        self.acvsup_rat(acvs_id, img)

    def acvs_uplen2(self, acvs_id, count):
        num = 1 if count == 0 else -1
        img = self.acvs_.find_one_and_update({'_id': acvs_id}, {"$inc": {'len2': num}}, upsert=True)
        img['len2'] = img['len2'] + num
        self.acvsup_rat(acvs_id, img)

    def acvs_uplen3(self, acvs_id, count):
        num = 1 if count == 0 else -1
        img = self.acvs_.find_one_and_update({'_id': acvs_id}, {"$inc": {'len3': num}}, upsert=True)
        img['len3'] = img['len3'] + num
        self.acvsup_rat(acvs_id, img)

    def acvsup_rat(self, acvs_id, img):
        rat1 = img['len1'] / img['lenall'] if img['len1'] > 0 else float(0)
        rat2 = img['len2'] / img['lenall'] if img['len2'] > 0 else float(0)
        rat12 = rat1 + rat2
        rat3 = img['len3'] / img['lenall'] if img['len3'] > 0 else float(0)
        self.acvs_.update_one({'_id': acvs_id},
                              {'$set': {'Ratio1': rat1, 'Ratio2': rat2, 'Ratio3': rat3, 'Ratio12': rat12}},
                              upsert=True)

    def acvs_iplenall(self, acvs_id):
        self.acvs_.find_one_and_update({'_id': acvs_id}, {"$inc": {'lenall': 1}}, upsert=True)

    def faceacvs_insert(self, img):
        self.faceacvs_.insert_one(img)

    def mdb_uptime(self, acvs_id, face_time):
        return self.acvs_.update({'_id': acvs_id}, {'$set': {'update_time': face_time}}, upsert=True)

    def insert_face_acvs(self, face_id, acvs_id, count, level):
        idname = '%s_%s' % (face_id, acvs_id)
        img = self.faceacvs_.find_one({'_id': idname})
        infoimg = redis_client.rdb_get_face(face_id)
        uptime = int(infoimg['timestamp'])
        if img:
            levelsrc = int(str(img['score'])[:-5])
            if level == levelsrc:
                self.faceacvs_upcount(idname, count)
            else:
                if level == 1:
                    if levelsrc == 2:
                        self.acvs_uplen2(acvs_id, 1)
                        self.acvs_uplen1(acvs_id, 0)
                        self.faceacvs_upcount(idname, count)
                        self.mdb_uptime(acvs_id, uptime)
                    if levelsrc > 2:
                        self.acvs_uplen3(acvs_id, 1)
                        self.acvs_uplen1(acvs_id, 0)
                        self.faceacvs_upcount(idname, count)
                        self.mdb_uptime(acvs_id, uptime)
                if level == 2:
                    if levelsrc > 2:
                        self.acvs_uplen3(acvs_id, 1)
                        self.acvs_uplen2(acvs_id, 0)
                        self.faceacvs_upcount(idname, count)
                        self.mdb_uptime(acvs_id, uptime)

                else:
                    self.faceacvs_upcount(idname, count)
        if not img:
            # 正脸最多只有两级档
            if level <= 2:
                self.acvs_tree_count(acvs_id)

            acvs_img = self.acvs_.find_one({"_id": acvs_id})

            del infoimg['_id']
            infoimg['face_id'] = face_id
            infoimg['acvs_id'] = acvs_id
            infoimg['p'] = acvs_img["p"]
            infoimg['score'] = count
            infoimg['_id'] = idname
            infoimg['timestamp'] = uptime
            infoimg['detectFacev2'] = int(infoimg['detectFacev2'])
            infoimg['detectFace'] = int(infoimg['detectFace'])
            infoimg['detectCount'] = int(infoimg['detectCount'])
            if 'x' in infoimg and 'y' in infoimg and 'z' in infoimg:
                infoimg['x'] = int(infoimg['x'])
                infoimg['y'] = int(infoimg['y'])
                infoimg['z'] = int(infoimg['z'])
            self.faceacvs_insert(infoimg)
            # 总长度
            self.acvs_iplenall(acvs_id)
            level_num = level == 1 and 1 or level == 2 and 2 or level > 2 and 3 or None
            if level_num:
                uplen_func = self.__getattribute__("acvs_uplen%s" % level_num)
                uplen_func(acvs_id, 0)

            self.mdb_uptime(acvs_id, uptime)

    def mdb_insert_acvs(self, acvs_id, acvs):
        acvs['_id'] = acvs_id
        self.acvs_.insert_one(acvs)
        self.acvs_tree_build(acvs_id, acvs)

    def upcard_lenacvs(self, card_id):
        return self.card_.update_one({'_id': card_id}, {"$inc": {'lenacvs': 1}}, upsert=True)

    def upcard_acvslen1(self, card_id, count):
        num = 1 if count == 0 else -1
        self.card_.update_one({'_id': card_id}, {"$inc": {'acvslen1': num}}, upsert=True)

    def upcard_acvslen2(self, card_id, count):
        num = 1 if count == 0 else -1
        self.card_.update_one({'_id': card_id}, {"$inc": {'acvslen2': num}}, upsert=True)

    def upcard_acvslen3(self, card_id, count):
        num = 1 if count == 0 else -1
        self.card_.update_one({'_id': card_id}, {"$inc": {'acvslen3': num}}, upsert=True)

    def up_acvs_len_card(self, acvs_id):
        return self.acvs_.update_one({'_id': acvs_id}, {"$inc": {'lencard': 1}}, upsert=True)

    def up_cardacvs_level(self, idname, level):
        return self.cardacvs_.update_one({'_id': idname}, {'$set': {'level': level}}, upsert=True)

    def up_cardacvs_sim(self, idname, high_sim, low_sim, aver_sim, count, similarity):
        if similarity > high_sim:
            self.cardacvs_.update_one({'_id': idname}, {'$set': {'similarity_high': similarity}}, upsert=True)
        if similarity < low_sim:
            self.cardacvs_.update_one({'_id': idname}, {'$set': {'similarity_low': similarity}}, upsert=True)
        new_count = count + 1
        new_aver_sim = int((aver_sim * count + similarity) / new_count)
        self.cardacvs_.update_one({'_id': idname}, {'$set': {'count': new_count, 'similarity_average': new_aver_sim}},
                                  upsert=True)

    # 更改 cardacvs 单向
    def mdb_acrd(self, card_id, acvs_id, similarity, level, face_id):
        idname = '%s_%s' % (card_id, acvs_id)
        img = self.cardacvs_.find_one({'_id': idname})
        faceimg = redis_client.rdb_get_face(face_id)

        face_time = int(faceimg['timestamp'])
        if img:
            # 更改相似度--以及card档的级别的数量
            scores = img['level']
            if level == 1:
                if scores == 2:
                    self.upcard_acvslen2(card_id, 1)
                    self.upcard_acvslen1(card_id, 0)
                if scores >= 3:
                    self.upcard_acvslen3(card_id, 1)
                    self.upcard_acvslen1(card_id, 0)
            if level == 2:
                if scores >= 3:
                    self.upcard_acvslen3(card_id, 1)
                    self.upcard_acvslen2(card_id, 0)

            if level < scores:
                self.up_cardacvs_level(idname, level)

            self.up_cardacvs_sim(idname, img['similarity_high'], img['similarity_low'], img['similarity_average'],
                                 img['count'],
                                 similarity)

            self.upcard_time(card_id, face_time)

        if not img:
            # db15redis
            imgall = redis_client.rdb_get_face(card_id)

            acvs_img = self.acvs_.find_one({"_id": acvs_id})

            del imgall['_id']
            imgall['_id'] = idname
            imgall['card_id'] = card_id
            imgall['p'] = acvs_img["p"]
            imgall['acvs_id'] = acvs_id
            imgall['similarity_high'] = similarity
            imgall['similarity_low'] = similarity
            imgall['similarity_average'] = similarity
            imgall['count'] = 1
            imgall['level'] = level
            imgall['card_timestamp'] = int(imgall['timestamp'])
            imgall['timestamp'] = face_time
            self.cardacvs_.insert_one(imgall)
            self.upcard_lenacvs(card_id)
            up_func = self.__getattribute__("upcard_acvslen%s" % level)
            up_func(card_id, 0)

            self.upcard_time(card_id, face_time)
            # 记录acvs--lencard
            self.up_acvs_len_card(acvs_id)
            self.mdb_uptime(acvs_id, face_time)

    def judge_card(self, card_id, facetime):
        img = self.card_.find_one({'_id': card_id})
        if not img:
            card_img = redis_client.rdb_get_face(card_id)
            self.card_.insert_one(
                {'_id': card_id, 'acvslen1': 0, 'acvslen2': 0, 'acvslen3': 0, 'len98': 0, 'len97': 0, 'len95': 0,
                 'len90': 0, 'len85': 0,
                 'Ratio98': float(0), 'Ratio97': float(0), 'Ratio95': float(0), 'Ratio90': float(0),
                 'Ratio85': float(0),
                 'lenacvs': 0, 'lenface': 0, 'created_time': facetime,
                 'update_time': facetime, 'face_image_uri': card_img['face_image_uri'], 'name': card_img['name'],
                 'person_id': card_img['person_id']})

    def upcard_time(self, card_id, face_time):
        return self.card_.update({'_id': card_id}, {'$set': {'update_time': face_time}}, upsert=True)

    def upcard_lenface(self, card_id):
        return self.card_.update({'_id': card_id}, {'$inc': {'lenface': 1}}, upsert=True)

    def card_rat(self, card_id, img):
        rat1 = img['len98'] / img['lenface'] if img['len98'] > 0 else float(0)
        rat2 = img['len97'] / img['lenface'] if img['len97'] > 0 else float(0)
        rat3 = img['len95'] / img['lenface'] if img['len95'] > 0 else float(0)
        rat4 = img['len90'] / img['lenface'] if img['len90'] > 0 else float(0)
        rat5 = img['len85'] / img['lenface'] if img['len85'] > 0 else float(0)
        self.card_.update_one({'_id': card_id},
                              {'$set': {'Ratio98': rat1, 'Ratio97': rat2, 'Ratio95': rat3, 'Ratio90': rat4,
                                        'Ratio85': rat5}},
                              upsert=True)

    def cardf(self, card_id, face_id, card_score):
        imgcard = redis_client.rdb_get_face(card_id)
        imgface = redis_client.rdb_get_face(face_id)
        facetime = int(imgface['timestamp'])
        self.judge_card(card_id, facetime)

        # card_face
        idname = '%s_%s' % (card_id, face_id)
        img = self.cardface_.find_one({'_id': idname})
        if img:
            self.cardface_.update_one({'_id': idname}, {"$inc": {'similarity': 0}}, upsert=True)
        if not img:
            imgall = {
                '_id': idname,
                'card_name': imgcard['name'],
                'card_id': card_id,
                'card_person_id': imgcard['person_id'],
                'card_timestamp': int(imgcard['timestamp']),
                'card_repository_id': imgcard['hezi_guidang_repo_id'],
                'card_face_image_id': imgcard['face_image_id'],
                'card_face_image_uri': imgcard['face_image_uri'],
                'card_picture_uri': imgcard['picture_uri'],
                'card_ip': imgcard['hezi_guidang_ip'],
                'face_id': face_id,
                'face_timestamp': facetime,
                'face_camera_id': imgface['camera_id'],
                'face_face_image_id': imgface['face_image_id'],
                'face_face_image_uri': imgface['face_image_uri'],
                'face_picture_uri': imgface['picture_uri'],
                'face_ip': imgface['hezi_guidang_ip'],
                'detectFacev2': int(imgface['detectFacev2']),
                'detectFace': int(imgface['detectFace']),
                'detectCount': int(imgface['detectCount']),
                'similarity': card_score

            }
            if ('x' in imgface) and ('y' in imgface) and ('z' in imgface):
                x_y_z = {
                    'x': int(imgface['x']),
                    'y': int(imgface['y']),
                    'z': int(imgface['z']),
                }
                imgall.update(x_y_z)

            self.cardface_.insert_one(imgall)

            self.upcard_lenface(card_id)

            card_score_level = (
                    card_score >= 98 and 98 or card_score >= 97 and 97 or card_score >= 95
                    and 95 or card_score >= 90 and 90 or card_score >= 85 and 85 or None)

            if card_score_level:
                img = self.card_.find_one_and_update({'_id': card_id}, {"$inc": {'len%s' % card_score_level: 1}},
                                                     upsert=True)
                img['len%s' % card_score_level] = img['len%s' % card_score_level] + 1
                self.card_rat(card_id, img)

            self.upcard_time(card_id, facetime)
